package club.gskj.system.controller;


import club.gskj.handler.BusinessException;
import club.gskj.response.Result;
import club.gskj.response.ResultCode;
import club.gskj.system.entity.User;
import club.gskj.system.service.AliOssService;
import club.gskj.system.service.UserService;
import club.gskj.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 康纳~
 * @since 2020-11-21
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块", tags = "系统用户接口")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/findUserPage")
    @ApiOperation(value = "用户列表分页", notes = "分页查所有用户信息")
    public Result findUserPage(
            @RequestParam(required = true, defaultValue = "1") Integer currentPage,
            @RequestParam(required = true, defaultValue = "6") Integer pageSize,
            @RequestBody UserVO userVO) {
        // 封装wrapper
        QueryWrapper<User> queryWrapper = getWrapper(userVO);
        Page<User> page = new Page<>(currentPage, pageSize);

        IPage<User> userPage = userService.findUserPage(page, queryWrapper);

        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "用户添加", notes = "添加用户信息")
    public Result createUser(@RequestBody User user) {
        try {
            userService.addUser(user);
            return Result.ok();
        } catch (Exception e) {
            // TODO 打印日志
            e.printStackTrace();
            return Result.error().data("message", e.getMessage());
        }
    }


    /**
     * 根据 vo对象， 封装查询对象wrapper
     */
    private QueryWrapper<User> getWrapper(UserVO userVO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userVO != null) {
            if (!StringUtils.isEmpty(userVO.getDepartmentId())) {
                queryWrapper.eq("department_id", userVO.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVO.getEmail())) {
                queryWrapper.eq("email", userVO.getEmail());
            }
            if (!StringUtils.isEmpty(userVO.getNickname())) {
                queryWrapper.eq("nickname", userVO.getNickname());
            }
            if (!StringUtils.isEmpty(userVO.getSex()) && !userVO.getSex().equals("2")) {
                queryWrapper.eq("sex", userVO.getSex());
            }
            if (!StringUtils.isEmpty(userVO.getUsername())) {
                queryWrapper.eq("username", userVO.getUsername());
            }
        }
        return queryWrapper;
    }

}


